<template>
  <div>
    <div class="product-list-header">
      <el-button 
        type="primary" 
        icon="el-icon-refresh" 
        :loading="loading"
        @click="refreshProducts"
        class="refresh-btn">
        刷新商品
      </el-button>
    </div>
    <el-row :gutter="20">
      <el-col :span="6" v-for="product in products" :key="product.productId">
        <el-card class="product-card" :body-style="{ padding: '0px' }">
          <img :src="getProductImage(product)" class="product-image">
          <div class="product-info">
            <h3 class="product-title">{{ product.title }}</h3>
            <div class="product-meta">
              <span class="publisher">发布者: {{ product.userNickname }}</span>
              <span class="price">¥{{ product.price }}</span>
            </div>
            <div class="product-status" :class="getStatusClass(product.status)">
              {{ product.status }}
            </div>
            <el-button type="primary" size="mini" @click.stop="goToDetail(product.productId)" style="margin-top: 10px;">查看详细</el-button>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <div v-if="loading" class="loading-container">
      <el-skeleton :rows="3" animated />
    </div>
    <div v-if="!loading && products.length === 0" class="empty-container">
      <el-empty description="暂无商品数据"></el-empty>
    </div>
  </div>
</template>

<script>
import ProductService from '@/Services/ProductService'

export default {
  name: 'ProductList',
  props: {
    status: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      products: [],
      loading: false,
      displayCount: 8
    }
  },
  created() {
    this.refreshProducts()
  },
  methods: {
    async refreshProducts() {
      if (this.loading) return
      this.loading = true
      try {
        const response = await ProductService.GetAll()
        if (response && response.data) {
          // 确保数据是数组
          const productList = Array.isArray(response.data) ? response.data : response.data.$values || []
          // 过滤商品状态
          let filtered = productList
          if (this.status) {
            filtered = filtered.filter(p => p.status === this.status)
          }
          // 随机打乱并限制显示数量
          this.products = filtered
            .sort(() => Math.random() - 0.5)
            .slice(0, this.displayCount)
        } else {
          this.$message.warning('获取商品数据为空')
          this.products = []
        }
      } catch (error) {
        console.error('获取商品数据失败:', error)
        this.$message.error('获取商品数据失败')
        this.products = []
      } finally {
        this.loading = false
      }
    },
    getProductImage(product) {
      if (product.imageBase64) {
        return `data:image/jpeg;base64,${product.imageBase64}`
      }
      return 'https://picsum.photos/300/200?random=' + Math.random()
    },
    getStatusClass(status) {
      switch (status) {
        case '在售':
          return 'status-onsale'
        case '已售出':
          return 'status-sold'
        case '已下架':
          return 'status-offline'
        default:
          return ''
      }
    },
    goToDetail(id) {
      this.$router.push({ name: 'ProductDetail', params: { id } })
    }
  }
}
</script>

<style scoped>
.product-list-header {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 10px;
}
.refresh-btn {
  background-color: #ffd700;
  color: #333;
}
.product-card {
  margin-bottom: 20px;
  transition: transform 0.3s;
}
.product-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}
.product-image {
  width: 100%;
  height: 200px;
  object-fit: contain;
  display: block;
  background-color: #f5f5f5;
  padding: 8px;
  box-sizing: border-box;
}
.product-info {
  padding: 14px;
}
.product-title {
  font-size: 16px;
  color: #333;
  margin: 0 0 10px 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.product-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 14px;
}
.publisher {
  color: #666;
}
.price {
  color: #f56c6c;
  font-weight: bold;
  font-size: 16px;
}
.loading-container {
  padding: 20px;
  text-align: center;
  background: #fff;
  margin-top: 20px;
}
.product-status {
  font-size: 12px;
  padding: 2px 6px;
  border-radius: 4px;
  margin-top: 8px;
  display: inline-block;
}

.status-onsale {
  background-color: #67C23A;
  color: white;
}

.status-sold {
  background-color: #F56C6C;
  color: white;
}

.status-offline {
  background-color: #909399;
  color: white;
}

.empty-container {
  padding: 40px;
  text-align: center;
  background: #fff;
  margin-top: 20px;
  border-radius: 4px;
}
</style> 